package com.erp.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Service;

import com.erp.dao.ErpbaseGroupDao;
import com.erp.data.ErpbaseGroup;
import com.erp.util.EntityManagerHelper;

@Service("groupService")
@RemotingDestination(channels = { "my-amf" })
public class GroupService {

	@RemotingInclude
	public boolean addGroup(ErpbaseGroup data) {

		ErpbaseGroupDao dao = new ErpbaseGroupDao();

		EntityManagerHelper.beginTransaction();
		System.out.println("a1");
		try {
			dao.save(data);
			EntityManagerHelper.commit();
		} catch (Exception e) {
			System.out.print(e.getMessage());
			EntityManagerHelper.rollback();
		} finally {
			EntityManagerHelper.closeEntityManager();
		}
		System.out.println("a2");
		return false;

	}

	@RemotingInclude
	public Object[] getUserArray(String a) {

		List<Map> result = new ArrayList<Map>();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", "GROUP");
		map.put("title", "Finance");

		List<Map> a1 = new ArrayList<Map>();
		Map<String, Object> m1 = new HashMap<String, Object>();
		m1.put("type", "GROUP");
		m1.put("title", "yu");
		a1.add(m1);
		Map<String, Object> m2 = new HashMap<String, Object>();
		m2.put("type", "USER");
		m2.put("title", "yanfeng");
		a1.add(m2);
		map.put("children", a1);

		result.add(map);

		JSONArray jsonArray = new JSONArray();

		jsonArray = JSONArray.fromObject(result);
		System.out.println("b");
		return jsonArray.toArray();
	}
}
